import { ref, watch } from "vue"

export function useUpdateMethods (fieldList:any) {
  const updateCheckBoxList = ref([]) //保存字段勾选状态的列表
  const upDateFieldList = ref<any>([]) //保存字段绑定数据的列表

  /**
   * 初始化更新列表
   */
  function initUpdateList () {
    
    let a = fieldList.value[0].fields.map((item: any) => {
      return {
        aliasName: '',
        ...item,
        isParamName: true
      }
    })
    upDateFieldList.value = a as any
    
  }
  const allUpdateCheckBox = ref(false)
  function changeUpdateAllCheckBox (e: any) {
    if (e) {
      updateCheckBoxList.value = upDateFieldList.value.map(
        (item: any) => item.value
      )
    }else{
      updateCheckBoxList.value = []
    }
  }
  watch(
    () => updateCheckBoxList.value,
    newVal => {
      allUpdateCheckBox.value =
        newVal.length === upDateFieldList.value.length && newVal.length > 0
    },
    {
      deep: true,
      immediate: true
    }
  )
  return {
    updateCheckBoxList,
    upDateFieldList,
    allUpdateCheckBox,
    changeUpdateAllCheckBox,
    initUpdateList,
  }
}
